info<-function(term, tag)
{
  print(term)
  re0<-paste("\\Q",tag,"\\E",sep="")
  re1<- paste("^",re0," | ",re0," | ",re0,"$","|^",re0,"$",sep="")
  ind.actual<-grep(re1, df_sample$Tags,perl=T)
  ec<-rep(0, length(df_sample$Tags))
  ec[ind.actual]<-1              ##1 if the tag is applicable to the document, else 0

  et<-as.integer(as.vector(dtm[,term])>0)

  N<-length(df_sample$Tags)
                                        #Nij = number of documents (presence of term, presence of class)
  N00 = sum((1 - et) * (1 - ec))
  N01 = sum((1 - et) * ec)
  N10 = sum(et * (1 - ec))
  N11 = sum(et * ec)
  N0.any = N00 + N01
  N1.any = N10 + N11
  Nany.0 = N00 + N10
  Nany.1 = N01 + N11

  N11/N * log2((N * N11)/(N1.any * Nany.1)) +
  N01/N * log2((N * N01)/(N0.any * Nany.1)) +
  N10/N * log2((N * N10)/(N1.any * Nany.0)) +
  N00/N * log2((N * N00)/(N0.any * Nany.0))
}

I<-unlist(mclapply(colnames(dtm), info, "windows", mc.cores=16))
names(I)<-colnames(dtm)
I2<-(sort(I, decreasing=T))[1:5]

tag<-"string"
re0<-paste("\\Q",tag,"\\E",sep="")
re1<- paste("^",re0," | ",re0," | ",re0,"$","|^",re0,"$",sep="")
ind.actual<-grep(re1, df_sample$Tags,perl=T)
N<-length(df_sample$Tags)
Nc<-length(ind.actual)
prior<-c(Nc/N, 1-Nc/N)
Nct<-apply(as.matrix(dtm[ind.actual,names(I2)]>0), 2, sum)
condprob<-(Nct+1)/(Nc+2)

Vd<-as.matrix(dtm[,names(I2)]>0)
p.tag<-log(prior[1]) + apply(log(condprob)*Vd[1:N,], 1, sum)
p.other<-log(prior[2]) + apply(log(1 - condprob) * (1 - Vd[1:N,]), 1, sum)

for(delta in seq(  5,10, by=  .1))
{
pred<-as.integer(p.tag <= p.other - delta)
act<-rep(0, N)
act[ind.actual]<-1
tp<-sum(act*pred)
print(c(tp/sum(pred),tp/sum(act),tp/sum(pred)*tp/sum(act)*2/(tp/sum(pred)+tp/sum(act))))
}
